package com.gds.wiki.cloud.admin.server.modular.sys.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gds.wiki.cloud.admin.client.sys.model.Resource;
import com.gds.wiki.cloud.admin.client.sys.model.query.ResourceQuery;
import com.gds.wiki.cloud.admin.client.sys.vo.vue.MenuVo;
import com.gds.wiki.cloud.admin.client.sys.vo.vue.MetaVo;
import com.gds.wiki.cloud.admin.server.modular.sys.mapper.ResourceMapper;
import com.gds.wiki.cloud.oauth2.admin.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceService extends ServiceImpl<ResourceMapper, Resource> {

     @Autowired
     private ResourceMapper resourceMapper;

    public List<MenuVo> getMenu(Long uid) {
       List<Resource> resources= resourceMapper.getMenuVoList(uid);
        return treeList(resources,0l);
    }

         public List<MenuVo> treeList(List<Resource> resourceList,Long parentId){
        List<MenuVo> list=null;
        for(Resource resource:resourceList){
            if(resource.getPid().longValue()==parentId.longValue()){
                MenuVo menuVo=new MenuVo();
                menuVo.setName(resource.getComponentName());
                menuVo.setComponentName(resource.getName());
                if(StrUtil.isEmpty(resource.getUrl())){
                    menuVo.setPath("");
                }
                else {
                    menuVo.setPath(resource.getUrl());
                }

                menuVo.setComponent(resource.getComponentPath());
                menuVo.setStatus(resource.getStatus());
                MetaVo metaVo =new MetaVo();
                metaVo.setIcon(resource.getIcon());
                metaVo.setTitle(resource.getName());
                menuVo.setMeta(metaVo);
                List<MenuVo> child=treeList(resourceList,resource.getId());
                menuVo.setChildren(child);
                if(list==null){
                    list=new ArrayList<>();
                }
                list.add(menuVo);
            }
        }
        return list;
    }

    public List<Resource> getList(ResourceQuery resourceQuery) {
         QueryWrapper wrapper=new QueryWrapper();
         Resource resource=new Resource();
         BeanUtil.copyProperties(resourceQuery,resource);
         ((QueryWrapper) wrapper).setEntity(resource);
      return resourceMapper.selectList(wrapper);
    }


         public List<Map> findDeptResource() {
        Long deptId= SecurityUtil.getSecurityUser().getDeptId();
        Map map=new HashMap();
        map.put("deptId",deptId);
        return resourceMapper.findDeptResource(map);
    }

        public List<Map> findUserResourceByUid(Long uid) {
        return resourceMapper.findUserResourceByUid(uid);
    }

}
